package com.ebt.m.customer.ui;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.customer.entity.CustomerSimple;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.buscomponent.listview.SectionItem;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Damon on 2017/11/02.
 * 30天活动客户presenter
 */

public class CustomerThirtyPresenter extends BaseListPresenter<BaseListContract.IView> {
    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    private List<SectionItem> items = new ArrayList<>();
    String mCustomerUuid;

    public CustomerThirtyPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.getCustomerAllActive()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(result -> {
                    items.clear();
                    List<CustomerSimple> list = result.getData();
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            SectionItem sectionItem = new SectionItem(111, list.get(i));
                            items.add(sectionItem);
                        }
                    }
                })
                .subscribe(sectionItems -> {
                            iView.updateData(items);
                        }
                        , throwable -> {
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.errorData();
                        });
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
    }

}
